namespace YXShop.ExcelLite
{
    using System;
    using System.Drawing;

    public sealed class ExcelCell : AbstractRange
    {
        private object cellValue;
        private CellStyle style;

        internal ExcelCell(ExcelWorksheet parent) : base(parent)
        {
        }

        internal ExcelCell(ExcelWorksheet parent, ExcelCell sourceCell) : base(parent)
        {
            this.cellValue = sourceCell.ValueInternal;
            this.Style = sourceCell.Style;
        }

        internal void AddToMergedRange(MergedCellRange mergedRange)
        {
            if ((mergedRange.Value == null) && (this.cellValue != null))
            {
                mergedRange.ValueInternal = this.cellValue;
                if (!((this.style == null) || this.style.IsDefault))
                {
                    mergedRange.Style = this.style;
                }
            }
            this.AddToMergedRangeInternal(mergedRange);
        }

        internal void AddToMergedRangeInternal(MergedCellRange mergedRange)
        {
            this.cellValue = mergedRange;
        }

        public static DateTime ConvertExcelNumberToDateTime(double num)
        {
            DateTime time = new DateTime(0x76c, 1, 1);
            if (double.IsNaN(num) || double.IsInfinity(num))
            {
                return time;
            }
            long num2 = (long) num;
            long num3 = (long) ((num - num2) * 86400.0);
            return time.AddDays((double) (num2 - 2L)).AddSeconds((double) num3);
        }

        internal void RemoveFromMergedRange()
        {
            MergedCellRange cellValue = this.cellValue as MergedCellRange;
            if (cellValue == null)
            {
                throw new Exception("Internal error: cell is not merged.");
            }
            this.cellValue = cellValue.ValueInternal;
            this.style = cellValue.Style;
        }

        public override void SetBorders(MultipleBorders multipleBorders, Color lineColor, LineStyle lineStyle)
        {
            this.Style.Borders.SetBorders(multipleBorders, lineColor, lineStyle);
        }

        public override string Formula
        {
            get
            {
                CellFormula valueInternal = this.ValueInternal as CellFormula;
                if (valueInternal != null)
                {
                    return valueInternal.Formula;
                }
                return null;
            }
            set
            {
                this.ValueInternal = new CellFormula(value, base.Parent);
            }
        }

        internal CellFormula FormulaInternal
        {
            get
            {
                object valueInternal = this.ValueInternal;
                if (valueInternal is CellFormula)
                {
                    return (CellFormula) valueInternal;
                }
                return null;
            }
            set
            {
                if (value != null)
                {
                    this.ValueInternal = value;
                }
                else
                {
                    this.ValueInternal = this.Value;
                }
            }
        }

        public override bool IsStyleDefault
        {
            get
            {
                if (this.style != null)
                {
                    return this.style.IsDefault;
                }
                return true;
            }
        }

        public CellRange MergedRange
        {
            get
            {
                if (this.cellValue is MergedCellRange)
                {
                    return (MergedCellRange) this.cellValue;
                }
                return null;
            }
        }

        public override CellStyle Style
        {
            get
            {
                if (this.cellValue is MergedCellRange)
                {
                    return ((MergedCellRange) this.cellValue).Style;
                }
                if (this.style == null)
                {
                    this.style = new CellStyle(base.Parent.ParentExcelFile.CellStyleCache);
                }
                return this.style;
            }
            set
            {
                if (this.cellValue is MergedCellRange)
                {
                    ((MergedCellRange) this.cellValue).Style = value;
                }
                else
                {
                    this.style = new CellStyle(value, base.Parent.ParentExcelFile.CellStyleCache);
                }
            }
        }

        public override object Value
        {
            get
            {
                object valueInternal = this.ValueInternal;
                if (valueInternal is CellFormula)
                {
                    valueInternal = ((CellFormula) valueInternal).Value;
                }
                return valueInternal;
            }
            set
            {
                if (value != null)
                {
                    ExcelFile.ThrowExceptionForUnsupportedType(value.GetType());
                }
                object valueInternal = this.ValueInternal;
                if (valueInternal is CellFormula)
                {
                    ((CellFormula) valueInternal).Value = valueInternal;
                }
                else
                {
                    this.ValueInternal = value;
                    base.CheckMultiline(value);
                }
            }
        }

        internal object ValueInternal
        {
            get
            {
                if (this.cellValue is MergedCellRange)
                {
                    return ((MergedCellRange) this.cellValue).ValueInternal;
                }
                return this.cellValue;
            }
            set
            {
                if (this.cellValue is MergedCellRange)
                {
                    ((MergedCellRange) this.cellValue).ValueInternal = value;
                }
                else
                {
                    this.cellValue = value;
                }
            }
        }
    }
}

